<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ page import="java.util.List" %>
<%@ page import="cn.edu.lingnan.pojo.Participation" %>
<%@ page import="cn.edu.lingnan.pojo.Competition" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <title>Title</title>
    <script type="text/javascript" src="../js/participation.js"></script>
    <link rel="stylesheet" type="text/css" href="../css/Part.css">
    <script type="text/javascript">
        // JavaScript function to select all checkboxes in a table
        function allcheck(tableId, checkbox) {
            var table = document.getElementById(tableId);
            var checkboxes = table.querySelectorAll('input[type="checkbox"]');
            checkboxes.forEach(function(cb) {
                cb.checked = checkbox.checked;
            });
        }
    </script>
</head>
<body>
<header>
    <h1>岭南师范学院学生竞赛系统管理页面----竞赛记录页面</h1>
    <hr>
</header>

<br><a href="/loginOut" class="link-style">注销</a>
<a href="/main.jsp" class="link-style">返回管理员首页</a>
<div class="container">
    <div class="search-container">
        <form action="/queryAllPart" method="get" id="partForm" name="allPartForm">
            <input type="text" id="partName" name="partName" placeholder="请输入查询的竞赛记录">
            <input type="submit" value="查询">
        </form>
    </div>
    <div id="approvedTable">

        <h2>审批报名</h2>

        <table id="approvedTableContent">
            <tr>
                <th><input type="checkbox" onclick="allcheck('approvedTableContent', this);"></th>
                <th>记录编号</th>
                <th>学生编号</th>
                <th>比赛编号</th>
                <th>比赛名称</th>
                <th>状态</th>
                <th>操作</th>
            </tr>
            <%
                List<Participation> allPart =(List<Participation>)session.getAttribute("allPart");
                boolean flag = false;
                String status="";
                if (allPart != null && !allPart.isEmpty()) {
                    for(Participation participation:allPart){
                        if (participation.getStatus() == 0 || participation.getStatus() == 2) {
                            flag = true;
                            if(participation.getStatus() == 0){
                                status="待审批";
                            } else if (participation.getStatus() == 1) {
                                status="已审批";
                            } else {
                                status="拒绝审批";
                            }
            %>
            <tr>
                <td><input type="checkbox" name="checkApproved" value="<%=participation.getId()%>"></td>
                <td><%=participation.getId()%></td>
                <td><%=participation.getStudent_id()%></td>
                <td><%=participation.getCompetition_id()%></td>
                <td><%=participation.getCompetition_name()%></td>
                <td><%=status%></td>
                <td>
                    <%
                        String url = "updatePart?pid=" + participation.getId() +
                                "&status=1" +
                                "&studentId=" + participation.getStudent_id() +
                                "&competitionId=" + participation.getCompetition_id() +
                                "&competitionName=" + participation.getCompetition_name() +
                                "&score=" + participation.getScore() +
                                "&rank=0";
                        String url2 = "updatePart?pid=" + participation.getId() +
                                "&status=2" +
                                "&studentId=" + participation.getStudent_id() +
                                "&competitionId=" + participation.getCompetition_id() +
                                "&competitionName=" + participation.getCompetition_name() +
                                "&score=" + participation.getScore() +
                                "&rank=0";
                    %>
                    <a href="<%=url%>" class="link-style">审批通过</a>|
                    <a href="<%=url2%>" class="link-style">审批拒绝</a>
                </td>
            </tr>
            <%
                        }
                    }
                }
            %>
        </table>

        <div style="text-align: right; margin-top: 20px;">
            <input type="submit" value="批量删除" onclick="delCheck3()">
        </div>
        <%
            if(!flag) {
        %>
        <img src="/images/entry2.png" alt="空空如也">
        <%
            }
        %>
    </div><hr>
    <%
        List<Competition> competitions = (List<Competition>)session.getAttribute("competitions");
        for (Competition competition : competitions) {
            String competitionName = competition.getName();
            List<Participation> participations = (List<Participation>) session.getAttribute("part_" + competitionName);
    %>
    <div id="participationTable">
        <h2 align="center">竞赛记录表--<%=competitionName%></h2>

        <table id="participationTableContent_<%=competitionName%>">
            <tr>
                <th><input type="checkbox" onclick="allcheck('participationTableContent_<%=competitionName%>', this);"></th>
                <th>记录编号</th>
                <th>学生编号</th>
                <th>比赛编号</th>
                <th>比赛名称</th>
                <th>分数</th>
                <th>排名</th>
                <th>操作</th>
            </tr>
            <%
                List<Participation> allPart2 = participations;
                boolean flag2 = false;
                if (allPart2 != null && !allPart2.isEmpty()) {
                    for(Participation participation:allPart2){
                        if (participation.getStatus() == 1) {
                            flag2 = true;
            %>
            <tr>
                <td><input type="checkbox" name="checkParticipation" value="<%=participation.getId()%>"></td>
                <td><%=participation.getId()%></td>
                <td><%=participation.getStudent_id()%></td>
                <td><%=participation.getCompetition_id()%></td>
                <td><%=participation.getCompetition_name()%></td>
                <td><%=participation.getScore()%></td>
                <td><%=participation.getCompetition_rank()%></td>
                <td>
                    <a href="updatePart.jsp?pid=<%=participation.getId()%>&rank=<%=participation.getCompetition_rank()%>" class="link-style">打分</a> |
                    <a href="delPart?pid=<%=participation.getId()%>" class="link-style">删除</a>
                </td>
            </tr>
            <%
                        }
                    }
                }
            %>
        </table>

        <div style="text-align: right; margin-top: 20px;">
            <input type="submit" value="批量删除" onclick="delCheck3()">
        </div>

        <%
            if(!flag2) {
        %>
        <img src="/images/entry2.png" alt="空空如也">
        <%
            }
        %>
    </div>
    <%
        }
    %>
    <a href="#top" class="link-style">返回顶部</a>
</div>

</body>
</html>